from fontTools.misc.loggingTools import deprecateArgument


class Dog:
    """一个表示狗的简单类"""

    def __init__(self, name):
        """初始化方法"""
        self.name = name
    def bark(self):
        """实例方法"""
        print(f"{self.name} 在叫：汪汪！")

my_dog = Dog("旺财")
my_dog.bark()

class Rectangle:
    """矩形类"""

    def __init__(self, width, height):
        self.width = width
        self.height =height
        self.area = width * height

    def calculate_perimeter(self):
        """计算周长"""
        return 2 * (self.width + self.height)

    def resize(self, new_width, new_height):
        """调整矩形大小"""
        self.width = new_width
        self.height = new_width * new_height

rect = Rectangle(5,3)
print(f"面积： {rect.area}")
print(f"周长： {rect.calculate_perimeter()}")
rect.resize(7,4)
print(f"新面积： {rect.area}")

class Student:
    """学生类"""
    def __init__(self, name, age=18, major="未定"):
        self.name = name
        self.age =age
        self.major = major
        self.courses = []


    def enroll(self, course):
        """选课方法"""
        self.courses.append(course)
        print(f"{self.nmae} 已选课：{course}")

student1 = Student("张三")
student2 = Student("李四",20,"计算机科学")


print(student1.major)
student1.enroll("Python编程")


class BankAccount:
    """银行账户类"""

    def __init__(self,account_holder, initial_balance=0):
        if initial_balance < 0:
            raise ValueError("初始余额不能为负")

        self.account_holder = account_holder
        self.balance = initial_balance
        self.transactions = []
    def deposit(self, amount):
        """存款"""
        if amount <= 0:
            print("存款金额必须大于0")
            return

        self.balance += amount
        self.transactions.append(f"存款： +{amount}")

    def withdraw(self, amount):
        """取款"""
        if amount <= 0:
            print("取款金额必须大于0")
            return
        if amount > self.balance:
            print("余额不足")
            return
        self.balance -= amount
        self.transactions.append(f"取款： -{amount}")


try:
    account = BankAccount("王五",1000)
    account.deposit(500)
    account.withdraw(200)
    print(f"当前余额： {account.balance}")
    print("交易记录：", account.transactions)
except ValueError as e:
    print(e)


class Car:
    wheel = 4
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

    @classmethod
    def change_wheels(cls, new_count):
        """修改类属性"""
        cls.wheels = new_count

car1 = Car("Toyota","Camry")
car2 = Car("Honda","Accord")

print(car1.wheels)
print(Car.wheels)

Car.change_wheels(6)
print(car2.wheels)


class SecretKeeper:
    """演示私有属性"""

    def __init__(self,public_info, secret_info):
        self.public = public_info
        self.__secret = secret_info
    def reveal_secret(self):
        """访问私有属性的方法"""
        return self.__secret


keeper = SecretKeeper("公开信息","机密信息")

print(keeper.public)
print(keeper.reveal_secret())
print(keeper._SecretKeeper__secret)


class GameCharacter:
    """游戏角色类"""

    MAX_HEALTH = 100

    def __init__(self, name, attack_power=10):
        self.name = name
        self.health = self.MAX_HEALTH
        self.attack_power = attack_power
        self.is_alive = True

    def attack(self, other):
        """攻击另一个角色"""
        if not self.is_alive:
            print(f"{self.name} 已经死亡，无法攻击")
            return
        print(f"{self.name}攻击了 {other.name}!")
        other.take_damage(self.attack_power)
    def take_damage(self, amount):
        """受到伤害"""
        if not self.is_alive:
            return
        self.health -= amount
        print(f"{self.name} 受到{amount} 点伤害")

        if self.health <= 0:
            self.die()

    def die(self):
        """角色死亡"""
        self.is_alive =False
        self.health = 0
        print(f"{self.name} 已经死亡！")

def heal(self, amount):
    """治疗角色"""
    if not self.is_alive:
        print(f"{self.name} 已死亡,无法治疗")
        return
    self. health = min(self.MAX_HEALTH, self.health + amount)
    print(f"{self.name} 恢复了 {amount} 点生命值，当前生命值： {self.health}")


def __str__(self):
    """定义对象的字符串表示"""
    status = "存活" if self.is_alive else"死亡"
    return f"{self.name} [生命值： {self.health}/{self.MAX_HEALTH}, 状态： {status}]"


hero = GameCharacter("英雄", attack_power=15)
monster = GameCharacter("怪物")

print(hero)

hero.attack(monster)
print(monster)

monster.attack(hero)

print(hero)

hero.heal(20)























